package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import com.itheima.common.Com.Category;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.vo.CategoryDto;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加
     * @param category
     * @return
     */
    @Override
    public boolean add(Category category) {
        //雪花id
        category.setId(IdUtil.getSnowflakeNextIdStr());
        category.setCreateUser("1");
        category.setUpdateUser("1");
        categoryMapper.add(category);
        return true;
    }

    /**
     * 修改
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {

        categoryMapper.update(category);
        return true;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public boolean delete(String id) {
        categoryMapper.delete(id);
        return true;
    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<Category> getall(Integer type) {
        List<Category> getall = categoryMapper.getall(type);
        return getall;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Category> page(Integer page, Integer pageSize) {

        PageInfo<Category> pageInfo = new PageInfo<Category>();

        Integer start = (page - 1) * pageSize;
        List<Category> page1 = categoryMapper.page(start, pageSize);
        pageInfo.setRecords(page1);
        pageInfo.setCurrent(start);
        pageInfo.setSize(pageSize);
       long count= categoryMapper.count();
        pageInfo.setTotal(count);
        return pageInfo;
    }

    @Override
    public List<CategoryDto> list() {
        List<CategoryDto> list = categoryMapper.list();
        return list;
    }
}
